import matplotlib.pyplot as plt
import numpy as np

'''----------------------------- functions -----------------------------------'''
def convert_and_remove_duplicates(array):
    # Convert array of int32 to list
    array_list = [list(arr) for arr in array]
    # Flatten the 2D list
    flattened_list = [item for sublist in array_list for item in sublist]
    # Remove duplicate elements
    unique_list = list(set(flattened_list))
    return unique_list

def find_internal_points(arr1, *arrs):
    # 创建布尔索引
    mask = np.ones(len(arr1), dtype=bool)
    for i, row in enumerate(arr1):
        for compare_row in np.concatenate(arrs):
            if np.array_equal(row, compare_row):
                mask[i] = False
                break
    # 使用布尔索引筛选出第一个数组中没有出现的元素
    result = arr1[mask]
    return result

def plot_points_xy(points, color="k", marker="."):
    figure = plt.figure(dpi=300)
    axis = figure.add_subplot(111)
    axis.scatter(points[:, 0], points[:, 1], s=0.1, color=color, marker=marker)
    plt.axis("scaled")  # 设置x轴和y轴相同的缩放比例
    plt.show()

def reorder_points(coordinates):
    # 计算中心
    center_x = coordinates[:, 0].mean()
    center_y = coordinates[:, 1].mean()
    # 计算角度
    angle = np.arctan2(coordinates[:, 1] - center_y, coordinates[:, 0] - center_x)
    
    # 按角度值排序
    points_indices = np.argsort(angle)
    
    ordered_points = coordinates[points_indices, :]
    return ordered_points